<?php

namespace App\Http\Controllers\V1\Api;


use App\Models\V1\Admins;
use App\Models\V1\Config;
use App\Models\V1\Database;
use App\Models\V1\Game;
use App\Models\V1\Games;
use App\Models\V1\SysRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class GamesController extends BaseController
{


    /**
     * 增加游戏-api
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

//        state 状态，1：创建，2：完善资料 3：提交代码 ，4：审核通过 5:审核失败
//        is_configured 是否配置(上传文件) 0未 1已'
        $model = new Games();
        $id = auth('box')->id();

        $file_arr = $request->input('file_addr', '');
        if ($file_arr) {
            $is_configured = 1;
        } else {
            $is_configured = 0;
        }

        $model->create([
            'users_id' => $id,
            'operate_id' => $request->input('operate_id', ""),
            'version_id' => $request->input('version_id', ""),
            'photo' => $request->input('photo', ""),
            'name' => $request->input('name', ""),
            'size' => $request->input('size', ""),
            'ip' => $request->input('ip', ""),
            'version' => $request->input('version', ""),
            'port' => $request->input('port', ""),
            'logo' => $request->input('logo', ''),
            'file_addr' => $file_arr,
            'file_name' => $request->input('file_name', ''),
            'database_id' => $request->input('database_id', ''),
            'is_configured' => $is_configured,
            'test_formal' => $request->input('test_formal', 2),
            'high_how' => $request->input('high_how', 0),
            'state' => $request->input('state', 1),
            'remark' => $request->input('remark', '')
        ]);

        return ajax_output(200, "成功");
    }

    /**
     * 游戏列表-api
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = games::query();
        $id = auth('box')->id();
        $query->where('state', '=', 1);

        if ($request->filled('test_formal')) {
            $query->where('test_formal', $request->input('test_formal', 1));
        }
        if ($request->filled('version')) {
            $query->where('version', 'like', '%' . $request->input('version') . '%');
        }
        if ($request->filled('name')) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
        $query->where('users_id', '=', $id);
        $pageSize = $request->input('pageSize', 10);
        $sort = $request->input('sort', 'id');
        $order = $request->input('order', 'desc');
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);

        $database_model = new Database();
        foreach ($data->items() as &$value) {
            $value->mysql_name = $database_model->where('id', $value['database_id'])->first();
        }


        return ajax_output(200, "成功", $data);
    }


    /**
     * 游戏详情-api
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Games::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新游戏-api
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $vo = Games::find($id);
        $user_id = auth('box')->id();


        $operate_id = $request->input('operate_id');
        $name = $request->input('name');
        $size = $request->input('size');
        $ip = $request->input('ip');
        $version = $request->input('version');
        $port = $request->input('port');
        $logo = $request->input('logo');
        $file_addr = $request->input('file_addr');
        $file_name = $request->input('file_name');
        $database_id = $request->input('database_id');

        $test_formal = $request->input('test_formal');



        //0未上架 1已上架(正式服用)
        $high_how = 1;

        $state = 1;
        $remark = $request->input('remark');
        $version_id = $request->input('version_id');
        $photo = $request->input('photo');

        $data['users_id'] = $user_id;

//        $data['high_how'] = $high_how;
        $data['state'] = $state;
//        test_formal 1正式  2测试
        if (!empty($test_formal)) {
            $data['test_formal'] = $test_formal;
        }else{
            $data['test_formal'] = 1;
        }

        if (!empty($operate_id)) {
            $data['operate_id'] = $operate_id;
        }


        if (!empty($name)) {
            $data['name'] = $name;
        }


        if (!empty($size)) {
            $data['size'] = $size;
        }


        if (!empty($ip)) {
            $data['ip'] = $ip;
        }

        if (!empty($version)) {
            $data['version'] = $version;
        }


        if (!empty($port)) {
            $data['port'] = $port;
        }


        if (!empty($logo)) {
            $data['logo'] = $logo;
        }

        if (!empty($file_addr)) {
            $data['file_addr'] = $file_addr;
            $data['is_configured'] = 1;
        } else {
            $data['is_configured'] = 0;
        }


        if (!empty($file_name)) {
            $data['file_name'] = $file_name;
        }

        if (!empty($database_id)) {
            $data['database_id'] = $database_id;
        }

        if (!empty($remark)) {
            $data['remark'] = $remark;
        }

        if (!empty($version_id)) {
            $data['version_id'] = $version_id;
        }

        $photo_type = $request->input('photo_type');
        if ( $photo_type==1) {
            $data['photo'] = $photo;
        }

        $vo->update($data);
        return ajax_output(200, "成功");
    }

    /**
     * 删除游戏-api
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Games::destroy($id);
        return ajax_output(200, "成功");
    }


    /**
     * 正式游戏切换-api
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function cut(Request $request, $id)
    {
        try {
            DB::beginTransaction();
            //test_formal 1正式  2测试
            $user_id = auth('box')->id();
            $games_model = new Games();
            $games_model->where('users_id', $user_id)->where('test_formal', 1)->update(['high_how' => 0]);
            $vo = Games::find($id);
            $vo->update([
                'users_id' => $user_id,
                'high_how' => 1,
            ]);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");
    }


    /**
     * 游戏列表-api
     *
     * @return \Illuminate\Http\Response
     */
    public function list(Request $request, $type)
    {
        // type 0 全部 1正式 2测试
        $query = Games::query();
        $id = auth('box')->id();
        if (!empty($type)) {
            $query->where('test_formal', '=', $type);
        }
        $query->where('state', '=', 1);
        //test_formal 1正式  2测试
        $query->where('test_formal', '=', 1);
        $query->where('users_id', '=', $id);

        $pageSize = $request->input('pageSize', 100);
        $sort = $request->input('sort', 'id');
        $order = $request->input('order', 'desc');
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


}
